#!/usr/bin/env python
import sys, os
from payment_class import Payment

#{{{def _create_dataset ( start_point, lines, rows, tot_rows ):
def _create_dataset ( start_point, lines, rows, tot_rows ):
    res = {}

    count = 0
    for v in rows:
        v [ "_pos" ] = start_point + count
        res [ "row%d" % count ] = v
        count += 1

    res [ 'rows' ] = tot_rows
    res [ 'from_row' ] = start_point
    res [ 'to_row' ] = start_point + count
    res [ 'lines' ] = lines

    return res
#}}}
def get_templates ( liwe ):
    n = Payment  ( liwe )
    return { "templates" : n.get_templates () }
#{{{def get_payment ( liwe, id_table.name = 0 ):
def get_payment ( liwe, id_payment = 0 ):
    """
    :INPUT:
		- id_payment:            SAFE_NUMBER 
    """
    n = Payment ( liwe )
    return { "payment" : n.get_payment ( id_payment = id_payment ) }
#}}}
#{{{def list_payment ( liwe, module = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
def list_payment ( liwe, module = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):

    """
    :INPUT:
		- module:			SAFE_DB_STRING
		- quant:			SAFE_NUMBER
		- _X_LINES:			SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:			SAFE_NUMBER
    """ 
    
    use_dataset = 0

    if _X_LINES != -1:
        quant = _X_LINES
        use_dataset = 1

    if _X_START_POINT:
        start_point = _X_START_POINT
    else:
        start_point = _X_PAGE * quant

    n = Payment ( liwe )
    resp = n.list_payment ( module = module, quant = quant, start_point = start_point, do_count = use_dataset )


    rows = resp [ 'rows' ]
    tot_rows =  resp [ 'tot_rows' ]

    if not use_dataset:
        return { 'payment': rows }

    return _create_dataset ( start_point, quant, rows, tot_rows )
#}}}
